import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import QtGraphicalEffects 1.12
import FluentUI 1.0
import GlobalConsts 1.0

// import Admin.Core 1.0
FluWindow {

    id: loginWindow
    title: qsTr("登录")
    width: 450
    height: 300
    fixSize: true
    // modality: Qt.ApplicationModal
    showMinimize: false
    showMaximize: false
    FluRectangle {
        id: head_view
        width: parent.width
        height: 40
        LinearGradient {
            anchors.fill: parent
            start: Qt.point(0, 0)
            end: Qt.point(width, height)
            gradient: Gradient {
                GradientStop {
                    position: 0.0
                    color: "#4768dc"
                }
                GradientStop {
                    position: 1.0
                    color: "#ac6cb2"
                }
            }
        }
        Row {
            anchors {
                verticalCenter: parent.verticalCenter
                left: parent.left
                leftMargin: 10
                horizontalCenter: parent.horizontalCenter
            }
            spacing: 10
            Image {
                width: 20
                height: 20
                visible: status === Image.Ready ? true : false
                source: "qrc:/res/image/image4.png"
                anchors.verticalCenter: parent.verticalCenter
            }
            FluText {
                text: SysConstants.displayName
                visible: true
                font.pixelSize: 20
                font.weight: Font.Bold
                color: "#ffffff"
                anchors.verticalCenter: parent.verticalCenter
            }
        }
    }
    GridLayout {
        anchors.centerIn: parent
        columns: 2
        rowSpacing: 15
        columnSpacing: 10
        // 错误提示
        Item {
            Layout.columnSpan: 2
            Layout.fillWidth: true
        }
        // 用户名行
        FluText {
            text: "用户名:"
            Layout.alignment: Qt.AlignRight
        }

        FluTextBox {
            id: usernameField
            placeholderText: "请输入用户名"
            Layout.preferredWidth: 200
            focus: true
            text: "superAdmin"
            onTextChanged: errorLabel.visible = false
        }

        // 密码行
        FluText {
            text: "密　码:"
            Layout.alignment: Qt.AlignRight
        }

        FluTextBox {
            id: passwordField
            placeholderText: "请输入密码"
            echoMode: TextInput.Password
            Layout.preferredWidth: 200
            text: "123456"
            onAccepted: handleLogin()
            onTextChanged: errorLabel.visible = false
        }
        Item {
            Layout.fillWidth: true
        }
        Item {
            height: 10
            Layout.alignment: Qt.AlignLeft
            FluText {
                id: errorLabel
                color: "red"
                visible: false
                anchors.centerIn: parent
            }
        }

        // 按钮行
        Item {
            Layout.fillWidth: true
        } // 占位符

        RowLayout {
            spacing: 20

            FluFilledButton {
                text: "登录"
                Layout.preferredWidth: 90
                onClicked: handleLogin()
            }

            FluButton {
                text: "取消"
                Layout.preferredWidth: 90
                onClicked: Qt.quit()
            }
        }
    }

    Connections {
        target: userService
        function onLoginResult(success, message) {
            if (success) {
                // 跳转到主界面等操作
                loginWindow.hide()
                FluRouter.navigate("/")
                FluRouter.removeWindow(loginWindow)
            } else {
                // 显示错误提示
                errorLabel.text = message
                errorLabel.visible = true
                passwordField.selectAll()
                passwordField.focus = true
            }
        }
    }

    function handleLogin() {
        // 输入验证
        if (usernameField.text.trim() === "") {
            errorLabel.text = "用户名不能为空"
            errorLabel.visible = true
            usernameField.focus = true
            return
        }

        if (passwordField.text.trim() === "") {
            errorLabel.text = "密码不能为空"
            errorLabel.visible = true
            passwordField.focus = true
            return
        }

        // 登录验证
        login()
    }

    closeListener: function (event) {
        Qt.quit()
    }
    function login() {
        userService.login(usernameField.text, passwordField.text)

        // return true //usernameField.text === "admin" && passwordField.text === "123456"
    }
}
